package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.share.wxapi.OpenLoginListener;
import com.offroader.share.wxapi.ShareUtils;
import com.offroader.utils.StringUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.PageBaseBean;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyApp;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;

public class CircleUserAddWechatFragment extends BaseProgressFragment {

	private int circleId;

	@Bind(R.id.id_circle_two_dim_code_img)
	protected ImageView twoDimCodeImage;

	@Bind(R.id.id_circle_name)
	protected TextView nameText;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_circle_user_add_wechat;
	}

	@Override
	protected void initParams() {

		//解析参数
		Bundle args = getArguments();
		circleId = args.getInt("circle_id", 0);

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {
		RequestParams params = new RequestParams();
		params.put("circle_id", circleId);
		ApiRequest.circleUserAddWechat.request(this, params);
	}

	@OnClick(R.id.id_circle_add_wechat)
	public void circleAddWechat() {

		final AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin() && tokenKeeper.getUser().getBindWechat() == 1) {//已登陆&已绑定
			
			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("circle_id", tokenKeeper.getUser().circleId());
			params.put("share_type", 1);

			ApiRequest.circleWecahtShare.request(this, params, HandlerType.waitting);

		} else if (tokenKeeper.isLogin()&& tokenKeeper.getUser().getBindWechat() == 0) {
			
			//授权绑定后分享
			String appId = MyApp.getInstance().getString(R.string.weichat_app_id);
			String appSecret = MyApp.getInstance().getString(R.string.weichat_app_secret);

			ShareUtils.loginWechat(getActivity(), appId, appSecret, new OpenLoginListener() {

				@Override
				public void onSuccess(String accessToken, String openId) {
					
					if (StringUtils.isNotBlank(accessToken) && StringUtils.isNotBlank(openId)) {

						RequestParams params = new RequestParams();
						params.put("uid", tokenKeeper.getOpenId());
						params.put("openid", openId);
						params.put("access_token", accessToken);

						ApiRequest.accountBindAndrShareWechat.request(this, params, HandlerType.waitting);

					}

				}
			});
			
			
		}

	}
	
	@Override
	public void onRequestResonse(PageBaseBean page) {
		super.onRequestResonse(page);
		
		//绑定成功后分享
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
		
		if(tokenKeeper.isLogin()){
			
			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("circle_id", tokenKeeper.getUser().circleId());
			params.put("share_type", 1);

			ApiRequest.circleWecahtShare.request(this, params, HandlerType.waitting);
			
		}
		
	}

	public ImageView getTwoDimCodeImage() {
		return twoDimCodeImage;
	}

	public TextView getNameText() {
		return nameText;
	}

	public static void go(Activity act) {
		MyPageHelper.circleUserAddWechat.showMyPage(act);
	}

	public static void go(Activity act, int circleId) {

		Bundle args = new Bundle();
		args.putInt("circle_id", circleId);
		MyPageHelper.circleUserAddWechat.showMyPage(act, args);

	}

}
